import java.util.Scanner;
import java.util.Arrays;

public class Main {
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("=== 双色球彩票验证程序 ===");
        System.out.println("欢迎使用双色球验证系统！");

        try {
            // 获取用户投注号码
            int[] userNumbers = getUserInput();
            int[] userRedBalls = Arrays.copyOf(userNumbers, 6);
            int userBlueBall = userNumbers[6];

            // 生成开奖号码
            Object[] drawNumbers = LotteryChecker.generateDrawNumbers();
            int[] drawRedBalls = (int[]) drawNumbers[0];
            int drawBlueBall = (int) drawNumbers[1];

            // 显示开奖结果
            displayDrawResult(drawRedBalls, drawBlueBall);

            // 验证中奖等级
            int prizeLevel = LotteryChecker.checkPrizeLevel(
                    userRedBalls, userBlueBall, drawRedBalls, drawBlueBall);

            // 显示中奖结果
            displayPrizeResult(prizeLevel);

        } catch (Exception e) {
            System.out.println("发生错误: " + e.getMessage());
        } finally {
            scanner.close();
        }
    }

    private static int[] getUserInput() {
        System.out.println("\n请输入您的双色球投注号码（6个红球+1个蓝球，空格分隔，如：01 05 12 18 26 33 09）：");

        while (true) {
            try {
                String input = scanner.nextLine().trim();
                String[] numberStrings = input.split("\\s+");

                if (numberStrings.length != 7) {
                    throw new IllegalArgumentException("请输入7个号码（6个红球 + 1个蓝球）");
                }

                int[] numbers = new int[7];
                for (int i = 0; i < 7; i++) {
                    numbers[i] = Integer.parseInt(numberStrings[i]);
                }

                int[] redBalls = Arrays.copyOf(numbers, 6);
                if (!LotteryChecker.validateRedBalls(redBalls)) {
                    throw new IllegalArgumentException("红球号码必须在1-33之间且不能重复");
                }

                int blueBall = numbers[6];
                if (!LotteryChecker.validateBlueBall(blueBall)) {
                    throw new IllegalArgumentException("蓝球号码必须在1-16之间");
                }

                return numbers;

            } catch (NumberFormatException e) {
                System.out.println("输入格式错误，请确保输入的是数字！");
                System.out.println("请重新输入（格式示例：01 05 12 18 26 33 09）：");
            } catch (IllegalArgumentException e) {
                System.out.println(e.getMessage());
                System.out.println("请重新输入：");
            }
        }
    }

    private static void displayDrawResult(int[] redBalls, int blueBall) {
        System.out.print("\n开奖结果：");
        for (int ball : redBalls) {
            System.out.print(LotteryChecker.formatNumber(ball) + " ");
        }
        System.out.println(LotteryChecker.formatNumber(blueBall));
    }

    private static void displayPrizeResult(int prizeLevel) {
        String prizeName = LotteryChecker.getPrizeName(prizeLevel);
        if (prizeLevel > 0) {
            System.out.println("恭喜！您中了" + prizeName + "！");
        } else {
            System.out.println("很遗憾，您" + prizeName + "。");
        }
    }
}